import Taro from '@tarojs/taro';
import { request } from '../request';

export function getGoodsList({ pid, nurse_type, hospital }: any) {
    return request.get<any>(`/nurse_goods/nurseGoods/listByWeChat?pid=${pid}&nurse_type=${nurse_type}&hospital=${hospital}`, {}, {
        useErrMsg: true
    });
}

export function getShopGoodsList({ pid, nurse_type, hospital }: any) {
    return request.get<any>(`/nurse_goods/nurseGoods/listByWeChat?pid=${pid}&shop_type=${nurse_type}&hospital=${hospital}`, {}, {
        useErrMsg: true
    });
}


export function getGoodsDetail({ goods_id, goods_type }: any) {
    return request.get<any>(`/nurse_goods_detail/nurseGoodsDetail/queryListByGoodsid?goodsId=${goods_id}&goods_type=${goods_type}`, {}, {
        useErrMsg: true
    });
}

export function getNightList() {
    return request.get<any>(`/night_fee/nightFee/list`, {}, {
        useErrMsg: true
    });
}

export function getUrgentList({ goods_id, goods_type }: any) {
    return request.get<any>(`/nurse_urgent/nurseUrgent/list`, {}, {
        useErrMsg: true
    });
}

export function getExpertList() {
    return request.get<any>(`/nurse_expert/nurseExpert/list`, {}, {
        useErrMsg: true
    });
}


export function getDistanceList(obj={}) {
    //loadTreeChildren queryListNew
    return request.get<any>(`/nurse_distance/nurseDistance/list`, obj, {
        useErrMsg: true
    });
}

export async function getFeeControl() {
    const res = await request.get<any>(`/fee_control/feeControl/list`, {
        hospital: ''
    }, {
        useErrMsg: true
    });
  
    if(res.success?.records?.length > 0){
        const ary = res.success?.records || [];
        const temp = Taro.getStorageSync('defaultHospital') || '{}'
        const hospital = JSON.parse(temp);
        for (let i = 0; i < ary.length; i++) {
            const item = ary[i];
            if(item.hospital == hospital.id){
                const str = item.fees || '';
                const list = str.split(',');
                const maps: any = {};
                list.forEach((i: any) => {
                    maps[i] = true;
                });
                return maps;
            }
        }

        
    }
    return {};
}

export function loadTreeChildren({ pid }: any) {
    return request.get<any>(`/nurse_goods/nurseGoods/loadTreeChildren?pid=${pid}`, {}, {
        useErrMsg: true
    });
}

export function loadShopTreeChildren({ goodsType, pid }: any) {
    // childList  loadTreeChildren
    // /jeecg-boot/medication/medicationShop/rootList
    // /medication/medicationShop/loadTreeChildren
    return request.get<any>(`/medication/medicationShop/childList?goodsType=${goodsType}&pid=${pid}&isValidate=1`, {}, {
        useErrMsg: true
    });
}

export function loadTreeChildrenNew({ pid }: any) {
    //loadTreeChildren queryListNew
    // /nurse_goods_detail/nurseGoodsDetail/queryListNew
    // /nurse_goods_detail/nurseGoodsDetail/queryListNew
    return request.get<any>(`/nurse_goods_detail/nurseGoodsDetail/queryListNew?pid=${pid}`, {}, {
        useErrMsg: true
    });
}

export function loadDistance() {
    //loadTreeChildren queryListNew
    // /nurse_goods_detail/nurseGoodsDetail/queryListNew
    return request.get<any>(`/nurse_distance/nurseDistance/list`, {}, {
        useErrMsg: true
    });
}




export function addMedicationOrder(obj: any) {
    return request.post<any>(`/medication_order/medicationOrder/addByWeChat`, obj, {
        useErrMsg: true
    });
}

export function payMedicationOrder(obj: any) {
    return request.post<any>(`/medication_order/medicationOrder/payByWeChat?outTradeNo=${obj.outTradeNo}`, obj, {
        useErrMsg: true
    });
}

export function getMedicationOrder(param: any) {
    return request.get<any>(`/medication_order/medicationOrder/list`, param || {}, {
        useErrMsg: true
    });
}

export function getMedicationOrderById(id: string) {
    return request.get<any>(`/medication_order/medicationOrder/queryById?id=${id}`, {}, {
        useErrMsg: true
    });
}

export function getMedicationOrderDetail(code: string) {
    return request.get<any>(`/medication_order_detail/medicationOrderDetail/list?orderId=${code}`, {}, {
        useErrMsg: true
    });
}

